import os
import tkinter as tk

from 运维工具.登录.人脸识别通过 import Result
# 假设这些导入路径正确
from 运维工具.登录.登录页 import Login
from 运维工具.登录.验证页 import Check
from 运维工具.登录逻辑.demo03获取图片验证码 import getCheckCode
from 运维工具.登录逻辑.调用入口 import start, getERCode, getERresult

if __name__ == '__main__':
    datas = {
        "Authorization": "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhbm9ueW1vdXMiLCJhdWQiOiIiLCJzZXJ2ZXIiOiJodHRwOi8vbG9jYWxob3N0OjMwMzQiLCJyb2xlIjoiIiwic3RhdHVzIjoiMyIsImlzcyI6Imluc3B1ciIsImp0aSI6ImQzMmViMjE0LWViNjYtNDM2Ni05ZTVlLWI1NWQwMWM5MDJlMiIsImlhdCI6MTYyMDk3MTQ3NCwiZXhwIjoxNjIxMDAwMjc0fQ.M8XAoxEpHX--OnAJaRhmTb8YkhFzO2mKoUUji74VcFpZstUCRnpURAh0WhkFjei98jddk1yvD-DHLxmu0OuvJg"
    }

    # 获取当前文件的目录并构建图片路径
    current_dir = os.path.dirname(os.path.abspath(__file__))
    img_dir = os.path.join(os.path.dirname(current_dir), 'tkinter', '运维工具', 'static', 'img')
    datas = start(**datas)

    # 登录页面
    login_page = tk.Tk()
    login_state = tk.BooleanVar(login_page, value=False)
    username = tk.StringVar(login_page, value='')
    password = tk.StringVar(login_page, value='')
    codes = tk.StringVar(login_page, value='')

    # 创建登录窗口
    app = Login(
        master=login_page,
        width=400,
        height=350,
        title='运维工具 v0.0.1', **{
            'login_state': login_state,
            'img_dir': img_dir,
            'username': username,
            'password': password,
            'codes': codes
        }
    )

    # 关键修复：在主循环结束后再获取值（窗口关闭后）
    login_page.mainloop()

    # 此时才能正确获取到用户输入的值
    datas['username'] = username.get()
    datas['password'] = password.get()
    datas['captcha'] = codes.get()

    print(datas)
    print(f"登录状态: {login_state.get()}")

    # 验证页面（仅在登录成功时显示）
    if login_state.get():
        datas = getERCode(**datas)

        check_login_page = tk.Tk()

        """
        在此处返回登录的结果  保留在内存之中
        
        """
        resp =tk.StringVar(check_login_page,'')
        check = Check(
            master=check_login_page,
            title='运维工具 v0.0.1',
            width=400,
            height=300, **{
                'img_dir': img_dir,
                'datas':datas,
                'resp': resp,
            }
        )

        check_login_page.mainloop()
        print("打印登录信息开始")
        print(resp.get())
        print("打印登录信息结束")

        login_over=tk.Tk()
        result = Result(master=login_over,**{
            'img_dir': img_dir,
            'title':'运维工具 v0.0.1',
            'width':400,
            'height':300,
        })
        login_over.mainloop()
        print(7877)



